import java.util.ArrayList;
import java.util.List;
import java.util.*;;

public class Main{
    public static void main(String[] args) {
        List flowers = new ArrayList<>();//创建一个list来存储各种花的的对象
        Scanner sc= new Scanner(System.in);//输入设置
        int choice=1;
        double ave = averageprice(flowers);
        //循环输入5种不通的花
        while(choice>0){
            
            System.out.println("========================================");
            System.out.println("========================================");
            System.out.println("==========0.退出========================");
            System.out.println("==========1.开始输入====================");
            System.out.println("==========2.排序========================");
            System.out.println("==========3.添加花朵====================");
            System.out.println("==========4.删除花朵====================");
            System.out.println("==========5.输出总价值和平均价格=========");
            System.out.println("==========6.查询花朵====================");
            System.out.println("==========7.高于平均价格的花朵数为=======");
            System.out.println("==========8.低于平均价格的花朵数为=======");
            System.out.println("==========9.修改花朵====================");
            System.out.println("==========10.售后服务===================");
            System.out.println("==========11.返回=======================");
            System.out.println("=======================================");
            System.out.println("=======================================");
            System.out.println("请输入你的选择");
            int mytry=sc.nextInt();
            switch(mytry){
                case 0:System.out.println("已选择0"); choice=mytry;break;
                case 1:System.out.println("已选择1"); 
                        System.out.println("请输入5种花朵的花名,价格,还有数量");
                        for(int i=0;i<5;i++){
                        flowers.add(new flower(sc.next(), sc.nextDouble(), sc.nextInt()));
                    }
                    Iterator it=flowers.iterator();
                    while(it.hasNext()){
                    System.out.println(it.next());
                     }
                    //ave = averageprice(flowers);
                    break;
                case 2:System.out.println("已选择2"); ShowSortflowers(flowers);break;
                case 3:System.out.println("已选择3"); flowers.add(new flower(sc.next(), sc.nextDouble(), sc.nextInt()));break;
                case 4:System.out.println("已选择4"); 
                    Iterator ic=flowers.iterator();
                    while(ic.hasNext()){
                    System.out.println(ic.next());
                 }
                    System.out.println("请输入你要删除的序号");
                    int choic1=sc.nextInt();
                    flowers.remove(choic1);
                    break;
                case 5:ave = averageprice(flowers);
                    System.out.println("总价格为"+allprice(flowers));
                    //averageprice(flowers);
                    break;
                case 7:System.out.println("高于平均价格的数量为"+highave(flowers, ave));
                        break;
                case 6: System.out.println("下面输入你想查询的花朵的姓名");
                        String findname=sc.next();
                        System.out.println(getap(flowers, findname));break;
                case 8:System.out.println("低于平均价格的数量为"+lowave(flowers, ave));break;
                case 9:System.out.println("已选择4"); 
                        Iterator i9=flowers.iterator();
                        int e=0;
                        while(i9.hasNext()){
                        System.out.println(e+"号:"+i9.next().toString());
                        e++;
                    }
                        System.out.println("请输入你要修改的花朵的号码");
                        int choic3=sc.nextInt();
                        flowers.remove(choic3);
                        System.out.println("请重新输入花朵信息");
                        flowers.add(new flower(sc.next(), sc.nextDouble(), sc.nextInt()));
                        System.out.println("修改完成");
                        while(i9.hasNext()){
                            System.out.println(e+"号:"+i9.next().toString());}
                        break;
                case 10:findday();break;
                case 11:break;
                default:break;
            }

        }
        
        //打印输入的内容
        //计算平均价格
        //System.out.println(allprice(flowers));
        //System.out.println(averageprice(flowers));
        
       
    }
    public static void findday(){

        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH)+1;
        int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
        System.out.println("今天是"+year + "年" + month + "月" + dayOfMonth + "日");
        cal.add(Calendar.DAY_OF_MONTH, 3);
        year = cal.get(Calendar.YEAR);
        month = cal.get(Calendar.MONTH);
        dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
        System.out.println("我们承诺保鲜三天到"+year + "年" + month + "月" + dayOfMonth + "日");
    }
    //查找花朵的方法
    public static String getap(List flowers,String naem){
        for(Object i:flowers){
            if(((flower) i).getName().equals(naem)){
                return ((flower) i).getName()+"花价格"+((flower) i).getPrice()+"共有"+((flower) i).getAmount()+"朵";
            }
        }
        return "我花呢？我不道啊";
    }
    //计算高价花
    public static int highave(List flowers,double ave){
        int amt=0;
        for(Object i:flowers){
            if(((flower) i).getPrice()>ave){
                amt+=((flower) i).getAmount();
            }
        }
        return amt;
    }
    //计算低价花
    public static int lowave(List flowers,double ave){
        int amt=0;
        for(Object i:flowers){
            if(((flower) i).getPrice()<ave){
                amt+=((flower) i).getAmount();
            }
        }
        return amt;
    }
    //排序
    public static void ShowSortflowers(List flowers){
        Scanner sc= new Scanner(System.in);//输入设置
        Iterator it = flowers.iterator();
        System.out.println("排序之前的顺序为");
        while(it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println("请选择你的排序方式");
        System.out.println("=====1.价格升序=====");
        System.out.println("=====2.价格降序=====");
        System.out.println("====3.鲜花数量====");
        System.out.println("=====4.名称长短=====");
        int choic2=sc.nextInt();
        switch(choic2){
            case 1:System.out.println("你选择了1");
            Collections.sort(flowers, new Comparator<flower>() {
                public int compare(flower o1, flower o2) {
                    return (int)o1.getPrice()-(int)o2.getPrice();
                }
            });break;
            case 2:System.out.println("你选择了2");
            Collections.sort(flowers, new Comparator<flower>() {
                public int compare(flower o1, flower o2) {
                    return (int)o2.getPrice()-(int)o1.getPrice();
                }
            });break;
            case 3:System.out.println("你选择了3");
            Collections.sort(flowers, new Comparator<flower>() {
                public int compare(flower o1, flower o2) {
                    return (int)o2.getAmount()-(int)o1.getAmount();
                }
            });break;
            case 4:System.out.println("你选择了4");
            Collections.sort(flowers, new Comparator<flower>() {
                public int compare(flower o1, flower o2) {
                    return (int)o2.getName().length()-(int)o1.getName().length();
                }
            });
        }
        
        System.out.println("排序后为");
        it=flowers.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
    }
    //计算总价值
    public static double allprice(List flowers){
        double all=0;
        for(Object i:flowers){
            double n=((flower) i).getPrice();
            all+=n*((flower) i).getAmount();
        }
        return all;
    }
    //计算平均价格
    public static double averageprice(List flowers){
        double all=allprice(flowers);
        double max=0,min=Math.pow(2, 32)-1;
        int n =0;
        for(Object i:flowers){
            max=Math.max(((flower) i).getPrice(),max);
            max=Math.min(((flower) i).getPrice(),min);
            n+=((flower) i).getAmount();
        }
        
        double average=all/n;
        System.out.println("所有花的平均价格为"+average+"花朵最高价为"+max+"花朵最低价为"+min);
        return average;
    }
}
//设置花对象
class flower{
    private String name;
    private double price;
    private int amount;
    //构造方法
    public flower(String name,double price, int amount){
        this.amount=amount;
        this.name = name;
        this.price=price;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public int getAmount() {
        return amount;
    }
    public String getName() {
        return name;
    }
    public double getPrice() {
        return price;
    }
    //重写tostring
    @Override
    public String toString(){
        return "花朵名为"+name+"花朵价格为"+price+"花朵数量为"+amount;
    }
}